<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增节')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-chapter-add">
        <input type="hidden" name="courseId" th:value="${courseId}">
        <input type="hidden" name="parentId" th:value="${chapterId}">
        <div class="form-group">
            <label class="col-sm-3 control-label">节名称：</label>
            <div class="col-sm-8">
                <input name="chapterName" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">节排序：</label>
            <div class="col-sm-8">
                <input name="chapterSort" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea name="remark" class="form-control"></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否允许试看：</label>
            <div class="col-sm-8">
                <div class="radio-box" th:each="dict : ${@dict.getType('sys_yes_no')}">
                    <input type="radio" th:id="${'sectionIfTry_' + dict.dictCode}" name="sectionIfTry"
                           th:value="${dict.dictValue}" th:checked="${dict.default}">
                    <label th:for="${'sectionIfTry_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">视频：</label>
            <div class="col-sm-8">
                <input type="hidden" name="sectionVideoUrl">
                <div class="file-loading">
                    <input class="form-control file-upload" id="sectionVideoUrl" name="file" type="file">
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    var prefix = ctx + "chapter/chapter"
    $("#form-chapter-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            add()
        }
    }

    function add() {
        var formData = new FormData();
        formData.append("courseId", $("[name=courseId]").val())
        formData.append("parentId", $("[name=parentId]").val())
        formData.append("chapterName", $("[name=chapterName]").val())
        formData.append("chapterSort", $("[name=chapterSort]").val())
        formData.append("remark", $("[name=remark]").val())
        formData.append("sectionIfTry", $("[name=sectionIfTry]:checked").val())
        if ($('#sectionVideoUrl')[0].files[0] != undefined) {
            formData.append('sectionVideo', $('#sectionVideoUrl')[0].files[0]);
        }
        $.ajax({
            url: prefix + "/addSaveSection",
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (result) {
                $.operate.successCallback(result);
            }
        });
    }

    $(".file-upload").fileinput({
        maxFileCount: 1,
        autoReplace: true,
        showUpload: false, //是否显示上传按钮
        dropZoneTitle: "拖拽文件到这里 …" + `<br>` + "仅支持单文件上传",
        showPreview: false,
    }).on('fileuploaded', function (event, data, previewId, index) {
        $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
    }).on('fileremoved', function (event, id, index) {
        $("input[name='" + event.currentTarget.id + "']").val('')
    })

    /*章节-新增-选择父章节树*/
    function selectChapterTree() {
        var options = {
            title: '章节选择',
            width: "380",
            url: prefix + "/selectChapterTree/" + $("#treeId").val(),
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var body = layer.getChildFrame('body', index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        layer.close(index);
    }
</script>
</body>
</html>